package com.demo.business.category;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 类别表
 * @create: 2022-02-25 23:30:02
 **/
@Service("category")
@Slf4j
public class CategoryService{

    @Resource
    CategoryDao categoryDao;

    /**
     * 添加类别表,获取lCategoryId数据
     *
     * @param cdoRequest - lCategoryId:服务id
     *                   - sCategoryName:服务名称
     *                   - sSynopsis:类别简介
     *                   - sThumbnail:缩略图
     *                   - iThumbnailStatus:缩略图状态 1.上架 2.下架
     *                   - dCteateTime:创建时间
     *                   - dUpdateTime:修改时间
     * @return
     * @author 
     * @create: 2022-02-25 23:30:02
     */
    @OpenApi
    public Return addCategory(CDO cdoRequest) {
        categoryDao.addCategory(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("lCategoryId")) {
            cdoResult.set("lCategoryId", cdoRequest.get("lCategoryId"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lCategoryId，更新类别表数据
     *
     * @param cdoRequest - lCategoryId:服务id
     *                   - sCategoryName:服务名称
     *                   - sSynopsis:类别简介
     *                   - sThumbnail:缩略图
     *                   - iThumbnailStatus:缩略图状态 1.上架 2.下架
     *                   - dCteateTime:创建时间
     *                   - dUpdateTime:修改时间
     * @return
     * @author 
     * @create: 2022-02-25 23:30:02
     */
    @OpenApi
    public Return updateCategoryById(CDO cdoRequest) {
        int affectedRows = categoryDao.updateCategoryById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lCategoryId，删除类别表数据
     *
     * @param cdoRequest - lCategoryId:服务id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:02
     */
    //@OpenApi
    public Return deleteCategoryById(CDO cdoRequest){
        int affectedRows = categoryDao.deleteCategoryById(cdoRequest.getLong("lCategoryId"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除类别表数据
     *
     * @param cdoRequest
     * @return
     * @author 
     * @create: 2022-02-25 23:30:02
     */
    //@OpenApi
    public Return deleteCategory(CDO cdoRequest) {
        int affectedRows = categoryDao.deleteCategory(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取类别表数据
     *
     * @param cdoRequest - lCategoryId:服务id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:02
     */
    @OpenApi
    public Return getCategoryById(CDO cdoRequest) {
        CDO cdoMapperResult = categoryDao.getCategoryById(cdoRequest.getLong("lCategoryId"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取类别表数据
     *
     * @param cdoRequest - lCategoryId:服务id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:02
     */
    @OpenApi
    public Return getCategory(CDO cdoRequest) {
        CDO cdoMapperResult = categoryDao.getCategory(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取类别表数据列表
     *
     * @param cdoRequest
     * @return
     * @author 
     * @create: 2022-02-25 23:30:02
     */
    @OpenApi
    public Return getCategoryList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = categoryDao.getCategoryList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = categoryDao.getCategoryListCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取类别表数据列表
     *
     * @param cdoRequest - lCategoryId:服务id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:02
     */
    @OpenApi
    public Return getCategoryListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = categoryDao.getCategoryListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = categoryDao.getCategoryListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
